/**
 *
 *  @auth xlx_good@qq.com
 *  @date 17/6/21.
 *
 */

requirejs.config({
    baseUrl: '../../',
    paths:  {
        avalon: 'javascript/avalon.min',
        jquery: 'js/plugins/jquery/jquery-2.1.1.min',
        pagination: 'lib/pagination/jquery.pagination',
        config: 'javascript/config',
        api: 'javascript/api',
        layer: 'js/plugins/layer1/layer',
        arg: 'javascript/arg'
    },
    shim: {
        pagination: {
            deps: ['jquery']
        },
        api: {
            deps: ['jquery']
        },
        layer: {
            deps: ['jquery']
        }
    }
});


(function(win, dom, requirejs, initVm){
    var vm;
    requirejs(['avalon', 'jquery', 'pagination', 'config', 'api', 'layer', 'arg'], function(){
        //初始化vm
        vm = initVm(dom);
    });

})(window, document, requirejs, function(dom){
    var vm = avalon.define({
        $id: 'container',
        form: {
			categoryId: null,
			saleSiteId: null,
			status: null,
			id: null,
			name: null,
			barcode: null,
			brandName: null,
			pageNum: 1,
			pageSize: 10
		},
		keyword: '',
		categoryList: [],
		wareCategoryList: function() {
			Api.wareCategoryList(function(resp) {
				if(resp.code == 0) {
					vm.categoryList = resp.data;
				} else {
					layer.msg(resp.msg);
				}
			});
		},
		siteList: [],
		supplierSiteList: function() {
			Api.supplierSiteList(function(resp) {
				if(resp.code == 0) {
					vm.siteList = resp.data;
				} else {
					layer.msg(resp.msg);
				}
			});
		},
		statusList: [
			{id: 1, name: '草稿'},
			{id: 5, name: '待审核'},
			{id: 6, name: '审核不通过'}
		],
		keywordList: {
			"id": "商品ID",
			"name": "商品名称",
			"barcode": "商品条码",
			"brandName": "商品品牌"
		},
		list: [],
        search: function(){
			for(var key in vm.keywordList) {
				vm.form[key] = null;
			}
			vm.form[$('#selKeyword').val()] = vm.keyword;
			Api.wareEditPage(vm.form, function(resp) {
				if(resp.code == 0) {
					if(vm.form.pageNum == 1) {
						setPagination(resp.data.total, resp.data.pageNum - 1, vm);
					}
					vm.list = resp.data.list;
				} else {
					layer.msg(resp.msg);
				}
			});
        },
		wareDrafts: function(wareId) {
			var form = {
				id: wareId
			}
			Api.wareDrafts(form, function(resp) {
				if(resp.code == 0) {
					layer.msg(resp.msg);
					vm.search();
				} else {
					layer.msg(resp.msg);
				}
			});
		},
		addNew: function() {
			location.href = './add-item.html';
		}
    })
	avalon.filters.filterStatus = function(status) {
		return wareStatusMap[status];
	}
	avalon.filters.filterOption = function(ware) {
		//1-草稿,2-未上架,3-已上架,4-已删除,5-待审核,6-审核不通过
		var params = {
			id: ware.id
		}
		if(ware.status == 1 || ware.status == 6) {
			return '<a href="'+Arg.url("./edit-item.html", params)+'">编辑</a>';
		}
		if(ware.status == 5) {
			return '<a href="'+Arg.url("./view-item.html", params)+'">查看</a>&nbsp;&nbsp;<a href="javascript:;" :click="@wareDrafts(ware.id)">撤回</a>';
		}
		return '';
	}
    avalon.scan(dom.body);
	vm.wareCategoryList();
	vm.supplierSiteList();
	vm.search();
    return vm;
});

/**
 * 设置翻页
 * @param <number> 总条数
 * @param <number> 当前页码
 * */
function setPagination(total, current, vm){
    $('.pagination').pagination(total,{
        items_per_page: 10, //  每页显示条数
        current_page: current,
        num_display_entries: 20, // 显示的页面个数
        next_text: '下一页',
        prev_text: '上一页',
        num_edge_entries: 2, //省略号后面显示的页码个数
        callback: function(index){
			if(vm.form.pageNum != (index + 1)){
				vm.form.pageNum = index + 1;
			    vm.search();
			}
        }
    });
}
